package com.linln.admin.SpringBoot.controller;

import com.linln.admin.SpringBoot.domain.Lab;
import com.linln.admin.SpringBoot.domain.Team;
import com.linln.admin.SpringBoot.service.LabService;
import com.linln.admin.SpringBoot.service.TeamService;
import com.linln.admin.SpringBoot.validator.TeamValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Airili
 * @date 2020/12/22
 */
@Controller
@RequestMapping("/SpringBoot/team")
public class TeamController {

    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;
    @Autowired
    private LabService labService;




    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("SpringBoot:team:index")
    public String index(Model model, Team team) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("team_name", match -> match.contains())
                .withMatcher("lab_id", match -> match.contains());

        // 获取数据列表
        Example<Team> example = Example.of(team, matcher);
        Page<Team> list = teamService.getPageList(example);

        // 封装数据
        List<Team> content = list.getContent();
        System.out.println("--------------"+content);
        for (Team team1 : content){
            team1.setName(userService.getById(team1.getTeacher_id()).getNickname());
            team1.setLabName(labService.getById(team1.getLab_id()).getName());
        }
        List<Lab> labs = labService.getList();
//        List<Team> typeList = teamService.getList();
//        model.addAttribute("types",typeList);
        model.addAttribute("labs", labs);
        model.addAttribute("list", content);
        model.addAttribute("page", list);
        Session session = SecurityUtils.getSubject().getSession();
        String username = (String) session.getAttribute("user");
        if(username.charAt(0)=='1'){
            return "/SpringBoot/team/stu_index";
        }
        return "/SpringBoot/team/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("SpringBoot:team:add")
    public String toAdd(Model model) {

        Session session = SecurityUtils.getSubject().getSession();
        String username = (String) session.getAttribute("user");
        List<Lab> labs = labService.getList();
        model.addAttribute("labs", labs);
        if(username.charAt(0)=='2'){
            model.addAttribute("username",username);
            return "/SpringBoot/team/tea_add";
        }

        return "/SpringBoot/team/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("SpringBoot:team:edit")
    public String toEdit(@PathVariable("id") Team team, Model model) {
        List<Lab> labs = labService.getList();
        model.addAttribute("labs", labs);
        model.addAttribute("team", team);
        return "/SpringBoot/team/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"SpringBoot:team:add", "SpringBoot:team:edit"})
    @ResponseBody
    public ResultVo save(@Validated TeamValid valid, Team team) {
        // 复制保留无需修改的数据
        if (team.getId() != null) {
            Team beTeam = teamService.getById(team.getId());
            EntityBeanUtil.copyProperties(beTeam, team);
        }
        String teacherName = team.getTeacherName();
        User byName = userService.getByName(teacherName);
        team.setTeacher_id(byName.getId());

        // 保存数据
        teamService.save(team);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("SpringBoot:team:detail")
    public String toDetail(@PathVariable("id") Team team, Model model) {
        model.addAttribute("team",team);
        return "/SpringBoot/team/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("SpringBoot:team:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (teamService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}